package com.wlr.study.leetcode;

import java.util.*;

/**
 * @author HustleLr
 * @version 1.0.0
 * @date 2023/10/2 23:03
 * @describe LeetCode380
 */
public class LeetCode380 {

    List<Integer> list;
    Map<Integer, Integer> indexMap;

    Random random;

    public LeetCode380() {
        this.list = new LinkedList<>();
        this.indexMap = new HashMap<>();
        this.random = new Random();
    }

    public boolean insert(int val) {
        if (indexMap.containsKey(val)) {
            return false;
        }
        list.add(val);
        indexMap.put(val, val);
        return true;
    }

    public boolean remove(int val) {
        if (!indexMap.containsKey(val)) {
            return false;
        }
        int index = indexMap.get(val);
        int last = list.get(list.size() - 1);
        list.set(index, last);
        indexMap.put(last, index);
        list.remove(list.size() - 1);
        indexMap.remove(val);
        return true;
    }

    public int getRandom() {
        int randomIndex = random.nextInt(list.size());
        return list.get(randomIndex);

    }

    public static void main(String[] args) {

    }
}
